package com.xmler.plugin.adbrocket.adb

import com.android.ddmlib.IDevice
import com.android.tools.idea.gradle.project.model.AndroidModuleModel
import com.intellij.openapi.project.Project
import org.jetbrains.android.facet.AndroidFacet
import org.jetbrains.android.util.AndroidUtils
import java.awt.TextArea
import javax.swing.JTextArea

class DeviceFetcher(private val project: Project, private val bridge: Bridge) {
    fun fetch(): DeviceResultList? {
        val facets = AndroidUtils.getApplicationFacets(project) ?: emptyList()
        val devices = bridge.cnnectedDevices()  ?: emptyList()

//        if (facets.isNotEmpty()) {
//            val facet = facets?.first()
//            val packageName = AndroidModuleModel.get(facet)?.applicationId ?: ""
//            return DeviceResultList(devices, facets, packageName)
//        }
        return DeviceResultList(devices, facets)

//            if (!bridge.isReady()){
//                // TODO ERROR
//                return null
//            }
//
//            val devices = bridge.cnnectedDevices()
////            if (devices.size == 1) {
//            if (devices.isNullOrEmpty()){
//                return null
//            }else {
//                return DeviceResult(devices, facets, packageName)
//            }
////            }else if (devices.size > 1) {
////                return
////            }
//        }
//        return null
    }

//    private fun getFacet(facets: List<AndroidFacet>): AndroidFacet? {
//        val facet: AndroidFacet?
//        if (facets.size > 1) {
////            facet = ModuleChooserDialogHelper.showDialogForFacets(project, facets)
////            if (facet == null) {
////                return null
////            }
//            facet = facets[0]
//        } else {
//            facet = facets[0]
//        }
//
//        return facet
//    }
}

data class DeviceResultList(val devices: List<IDevice>, val facet: List<AndroidFacet>)
data class DeviceResult(val device: IDevice, val facet: AndroidFacet, val packageName: String, val display: JTextArea)